/********************************************************************************
* writer: vico
* create date: 2018-1-31
* description:封装二维码模块FNScanner,需要  privacy模块检查权限
* 暴露三个方法 fnStartAJPush  fnStopAJPush   fnChangeBindTags
*********************************************************************************/

(function(window){
  'use strict'
  var tPcallBcak ;
  var slideLeft = function(pCallBack){
        this.tPcallBcak = pCallBack;
        this._init(pCallBack);
   }

  slideLeft.prototype = {
    _init:function(pCallBack){//样式初始化
       var tListItemsBox = $api.domAll($api.byId('cusLists'),'.list-item-box');
       var tWidth = api.frameWidth;
       var tLength = tListItemsBox.length;
       for(var i=0;i<tListItemsBox.length;i++){
          var tCanSlide = $api.hasCls(tListItemsBox[i], 'canslide');
          var tListItem = $api.dom(tListItemsBox[i],'.list-item');
          var tListItemsBoxWidth = tWidth;
          $api.css(tListItem, 'width:' + tWidth + 'px');
          if(tCanSlide){//如果该栏目可以滑动，添加删除按钮
             var tItemH = $api.offset(tListItemsBox[i]).h;
             if(pCallBack){
                  $api.append(tListItemsBox[i], '<div class="list-btn" onclick="'+ pCallBack +'(this)">删除</div>');
             }
             var tBtn = $api.dom(tListItemsBox[i], '.list-btn');
             $api.css(tBtn, 'height:'+ tItemH +'px;');
             var tBtnW = $api.offset($api.dom(tListItemsBox[i],'.list-btn')).w;
             tListItemsBoxWidth = tWidth + tBtnW;
             $api.css(tListItemsBox[i],'width:'+ tListItemsBoxWidth +'px');
             this._fnAddEventListener(tListItemsBox[i]);
          }
       }
    },
    _fnAddEventListener:function(pEl){//增加时间监听
      var tLastX, tLastXForMobile;//最后点击的一个item
      var tPressedObj, tLastLeftObj;
      var tStart; //按下的点

      pEl.addEventListener('touchstart', function(e) {
          tLastXForMobile = e.changedTouches[0].pageX;
          tPressedObj = this; //按下的对象
          // 记录开始按下时的点
          var touches = event.touches[0];
          tStart = {
              x: touches.pageX, // 横坐标
              y: touches.pageY // 纵坐标
          };
      })

      pEl.addEventListener('touchmove', function(e) {
          //计算滑动过成功xy变化
          var touches = event.touches[0];
          delta = {
              x: touches.pageX - tStart.x,
              y: touches.pageY - tStart.y
          }

          //横向位移大于纵向位移，禁止纵向滚动
          if (Math.abs(delta.x) > Math.abs(delta.y)) {
              event.preventDefault();
          }
      });

      pEl.addEventListener('touchend', function(e) {
          if (tLastLeftObj && tPressedObj != tLastLeftObj) { //点击除当前做滑块外其他区域
              $api.removeCls(tLastLeftObj, 'pull-left-animate');
              $api.addCls(tLastLeftObj,'pull-right-animate');
              tLastLeftObj = null;
          }
          var diffX = e.changedTouches[0].pageX - tLastXForMobile;
          if(diffX < -100){
             if($api.hasCls(tPressedObj, 'pull-right-animate')){
               $api.removeCls(tPressedObj, 'pull-right-animate')
             }
             $api.addCls(tPressedObj, 'pull-left-animate');
             if(tLastLeftObj && tLastLeftObj != tPressedObj){
               $api.removeCls(tLastLeftObj, 'pull-left-animate');
               $api.addCls(tLastLeftObj, 'pull-right-animate');
             }
              tLastLeftObj = tPressedObj;
          }else if(diffX > 100){
             if(tPressedObj == tLastLeftObj){
               $api.removeCls(tPressedObj, 'pull-left-animate');
               $api.addCls(tPressedObj, 'pull-right-animate');
               tLastLeftObj = null;
             }
          }
      })

    },
    initList:function(){
        this._init(tPcallBcak);
    }
  }
    window.slideLeft = slideLeft;
})(window)
